package com.whfc.fmam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.enums.DelFlag;
import com.whfc.common.enums.EnableState;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.PageUtil;
import com.whfc.fmam.dao.FmamWarehouseMapper;
import com.whfc.fmam.entity.bo.FmamWarehouse;
import com.whfc.fmam.entity.dto.FmamWarehouseDTO;
import com.whfc.fmam.entity.qo.WarehouseAddQO;
import com.whfc.fmam.entity.qo.WarehouseEditQO;
import com.whfc.fmam.service.FmamWarehouseService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ；likang
 * @Description:
 * @Date:Create：in 2019/11/14 14:52
 * @Version：1.0
 */
@DubboService(interfaceClass = FmamWarehouseService.class, version = "1.0.0")
public class FmamWarehouseServiceImpl implements FmamWarehouseService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private FmamWarehouseMapper warehouseMapper;

    @Override
    public PageData<FmamWarehouseDTO> listFmamWarehouseDTO(Integer pageNum, Integer pageSize, Integer deptId) throws BizException {
        logger.info("查询仓库列表服务，pageNum:{},pageSize:{},deptIds:{}", pageNum, pageSize, deptId);
        PageHelper.startPage(pageNum, pageSize);
        List<FmamWarehouseDTO> list = warehouseMapper.selectFmamWarehouseByDeptId(deptId);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public List<FmamWarehouseDTO> listFmamWarehouseDTO(Integer deptId) throws BizException {
        List<FmamWarehouseDTO> list = warehouseMapper.selectFmamWarehouseByDeptId(deptId);
        list = list.stream()
                .filter(fmamWarehouseDTO -> fmamWarehouseDTO.getEnableFlag() == 1)
                .collect(Collectors.toList());
        return list;
    }

    @Override
    public void addWarehouse(WarehouseAddQO warehouseAddQO) throws BizException {
        logger.info("新增仓库服务，warehouseAddQO:{}", warehouseAddQO.toString());
        Integer deptId = warehouseAddQO.getDeptId();
        String code = warehouseAddQO.getCode();
        FmamWarehouse existWarehouse = warehouseMapper.selectByDeptIdAndCode(deptId, code);
        if (existWarehouse != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "仓库编码重复");
        }
        FmamWarehouse fmamWarehouse = new FmamWarehouse();
        BeanUtils.copyProperties(warehouseAddQO, fmamWarehouse);
        fmamWarehouse.setEnableFlag(EnableState.ENABLED.getValue());
        fmamWarehouse.setDelFlag(DelFlag.UNDELETE.getValue());
        warehouseMapper.insert(fmamWarehouse);
    }

    @Override
    public void editWarehouse(WarehouseEditQO warehouseEditQO) throws BizException {
        logger.info("编辑仓库服务，warehouseEditQO:{}", warehouseEditQO.toString());
        Integer id = warehouseEditQO.getId();
        Integer deptId = warehouseEditQO.getDeptId();
        String code = warehouseEditQO.getCode();
        FmamWarehouse existWarehouse = warehouseMapper.selectByDeptIdAndCode(deptId, code);
        if (existWarehouse != null && !id.equals(existWarehouse.getId())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "仓库编码重复");
        }
        FmamWarehouse warehouse = new FmamWarehouse();
        BeanUtils.copyProperties(warehouseEditQO, warehouse);
        warehouse.setUpdateTime(new Date());
        warehouseMapper.updateByPrimaryKeySelective(warehouse);
    }

    @Override
    public void enableWarehouse(Integer id) throws BizException {
        logger.info("启用仓库服务，id:{}", id);
        warehouseMapper.updateEnableById(id);
    }

    @Override
    public void disableWarehouse(Integer id) throws BizException {
        logger.info("禁用仓库服务，id:{}", id);
        warehouseMapper.updateDisableById(id);
    }

    @Override
    public void deleteWarehouse(Integer id) throws BizException {
        logger.info("逻辑删除仓库，id：{}", id);
        FmamWarehouse data = warehouseMapper.selectByPrimaryKey(id);
        if (data == null || data.getDelFlag() == 1) {
            return;
        }
        AssertUtil.isTrue(data.getEnableFlag() != null && data.getEnableFlag() == 1, "仓库启用状态下不能删除");
        warehouseMapper.deleteLogicById(id);
    }
}
